<?php 
class fileRenameAction extends AdminMasterUploadControl{
	public function execute($request){
		//check authentiaction
		$directory_path  	= $request->getParameter('path','');
		$name				= $request->getParameter('name','');
		$url = 'path='.$directory_path.'&name='.$name;
		$is_error = 0; // no thing
		$new_name = '';
		if($request->isMethod('POST')){
			$new_name = $request->getParameter('new_name','');
			if(empty($new_name)){
				$is_error = 2; // file name is empty
			}else{
				$old_path = sfConfig::get('sf_upload_dir').'/'.$directory_path.'/'.$name;
				if(!file_exists($old_path)){
					$is_error = 3; //file is not exist
				}else{
					$arr_name = explode(".", $name);
					$new_path = sfConfig::get('sf_upload_dir').'/'.$directory_path.'/'.$new_name.'.'.$arr_name[count($arr_name)-1];
					if(file_exists($new_path)){
						$is_error = 4; //file name existing
					}else{
						@rename($old_path, $new_path);
						$is_error = 1; //success
					}
				}
			}
		}else{
			$arr_name = explode(".", $name);
			$new_name = '';
			$sepa = '';
			for($i = 0; $i < count($arr_name)-1; $i++){
				$new_name .= $sepa.$arr_name[$i];
				$sepa = '.';
			}
		}
		$this->setVar('name', $new_name);
		$this->setVar('is_error', $is_error);
		$this->setVar('url', $url);
	}
}